﻿/// <reference path="../../Scripts/jquery-2.0.0-vsdoc.js" />

/*!
* jQuery bigpipe Library v1.0.0
* http://vcoded.com/
*
*
* Copyright 2013, 2020 vcoded.com
* Released under the GPL license
*
* Date: 2013-06-1
*/

(function ($) {

    $.vcoded = $.vcoded || {};

    if ($.vcoded.version) {
        return;
    }

    //#region vcoded config
    $.extend($.vcoded, {
        version: "1.0.0",
        auth: 'vcoded.com',

        pagelet: {
            key: '',
            pid: '',
            elem: undefined,
            css: [],
            js: [],
            html: ''
        },

        pageletOpts: {
            elem: undefined,
            loading: undefined,
            reload: false,
            append: false,
            url: '',
            fn: undefined,
            cache: true,
            jsAsync: true
        }
    });
    //#endregion

    //#region JQuery extend
    $.extend({
        //isArray
        isArray: function (value) {
            return Object.prototype.toString.call(value) === '[object Array]';
        },
        includePath: '',
        include: function (file, async) {
            var files = typeof file == "string" ? [file] : file;
            var scripts = [];
            for (var i = 0; i < files.length; i++) {
                if (files[i] == '')
                    continue;
                var name = files[i].replace(/^\s|\s$/g, "");
                var att = name.split('.');
                var ext = att[att.length - 1].toLowerCase();
                var isCSS = ext == "css";
                var tag = isCSS ? "link" : "script";
                var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
                var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
                if ($(tag + "[" + link + "]").length == 0) {
                    var obj = $("<" + tag + attr + link + "></" + tag + ">");
                    obj.appendTo("head");
                    if (tag == "script")
                        scripts.push(obj);
                }
            }
        },
        isNullOrWhiteSpace: function (input) {
            return input === undefined || input == null || jQuery.trim(input) == '';
        }
    });

    $.extend({
        hashtable: {
            items: new Array(),
            itemsCount: 0,
            add: function (key, value) {
                if (!this.containsKey(key)) {
                    this.items[key] = value;
                    this.itemsCount++;
                }
                else {
                    this.items[key] = value;
                }
            },
            get: function (key) {
                if (this.containsKey(key))
                    return this.items[key];
                else
                    return null;
            },
            remove: function (key) {
                if (this.containsKey(key)) {
                    delete this.items[key];
                    this.itemsCount--;
                }
                else
                    throw "key '" + key + "' does not exists."
            },
            containsKey: function (key) {
                return typeof (this.items[key]) != "undefined";
            },
            containsValue: function containsValue(value) {
                for (var item in this.items) {
                    if (this.items[item] == value)
                        return true;
                }
                return false;
            },
            contains: function (key) {
                return this.containsKey(key);
            },
            clear: function () {
                this.items = new Array();
                itemsCount = 0;
            },
            size: function () {
                return this.itemsCount;
            },
            isEmpty: function () {
                return this.size() == 0;
            }
        }
    });
    //#endregion

    //#region pagelet functions
    function renderPagelet(pagelet, opts) {
        if (!$.isNullOrWhiteSpace(pagelet.css)) {
            $.include(pagelet.css);
        }
        if (!$.isNullOrWhiteSpace(pagelet.html)) {
            var elem;
            if (!$.isNullOrWhiteSpace(pagelet.pid)) {
                elem = $("#" + pagelet.pid);
            }
            if (opts && opts.elem != undefined) {
                elem = opts.elem;
            }
            if (opts && opts.append) {
                elem.append(pagelet.html);
            }
            else {
                elem.html(pagelet.html);
            }
        }
        if (!$.isNullOrWhiteSpace(pagelet.js)) {
            var async = opts ? opts.jsAsync : true;
            $.include(pagelet.js, async);
        }
        if (opts && opts.fn)
            opts.fn.call(this, pagelet);
    };
    //#endregion

    //#region bigPipe
    $.extend($.vcoded, {
        bigPipe: function (pagelets, opts) {
            if (!$.isArray(pagelets))
                return;

            jQuery.each(pagelets, function (i, n) {
                setTimeout(function () {
                    renderPagelet(n, opts);
                });
            });
        },

        pageLet: function (opts) {
            opts = $.extend({}, $.vcoded.pageletOpts, opts);
            if ($.hashtable.containsKey(opts.url)) {
                if (opts.reload) {
                    var json = $.hashtable.get(opts.url);
                    $.vcoded.bigPipe(json, opts);
                }
                return;
            }

            if (opts.elem && opts.loading)
                opts.elem.html(opts.loading);

            $.ajax({
                dataType: "json",
                url: opts.url,
                cache: false,
                success: function (json) {
                    $.vcoded.bigPipe(json, opts);
                    if (opts.cache) {
                        $.hashtable.add(opts.url, json);
                    }
                }
            });
        }
    });
    //#endregion

})(jQuery);